package com.jstyle.jclifexd.activity;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.daoManager.UserInfoDaoManager;
import com.jstyle.jclifexd.model.RecordData;
import com.jstyle.jclifexd.model.Recordmian;
import com.jstyle.jclifexd.utils.ChartDataUtil;
import com.jstyle.jclifexd.utils.DateUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * 成长记录
 */
public class GrowthRecordActivity extends BaseActivity {

    @BindView(R.id.distance)
    TextView distance;
    @BindView(R.id.pace)
    TextView pace;
    @BindView(R.id.heartRate)
    TextView heartRate;
    @BindView(R.id.stride)
    TextView stride;


    @BindView(R.id.bottonstype)
    Button bottonstype;//年月类型
    @BindView(R.id.title)
    Button title;//标题
    @BindView(R.id.LineChartView_recordt)
    LineChartView ChartView_recordt;
    int monthtype=0;//默认当前显示的是月
    boolean ismyself=false;//是否自己查看自己的数据

    boolean distancecheck=true;
    boolean pacecheck=true;
    boolean heartRatecheck=true;
    boolean stridecheck=true;
    List<RecordData> data;
    String uid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_records);
        ButterKnife.bind(this);
        //判断是不是自己查看自己的数据
        if(null!=getIntent()){
            if("my".equals(getIntent().getStringExtra("my"))){
                ismyself=true;
                title.setText(UserInfoDaoManager.getUserByUid(NetWorkUtil.getUserId()).getName());
                uid=NetWorkUtil.getUserId();
            }else{
                uid=getIntent().getStringExtra("uid");
                title.setText(getIntent().getStringExtra("uname"));
            }
        }

        QueryUserState(monthtype);

    }




    @OnClick({R.id.iv_back,R.id.bottonstype,R.id.distance,R.id.pace,R.id.heartRate,R.id.stride})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.distance:
                if(distancecheck){
                    distancecheck=false;
                    settigDrawable(R.drawable.distance_unselect,distance);
                }else{
                    distancecheck=true;
                    settigDrawable(R.drawable.distance_select,distance);
                }
                updta();
                break;
            case R.id.pace:
                if(pacecheck){
                    pacecheck=false;
                    settigDrawable(R.drawable.pace_unselect,pace);
                }else{
                    pacecheck=true;
                    settigDrawable(R.drawable.pace_select,pace);
                }
                updta();
                break;
            case R.id.heartRate:
                if(heartRatecheck){
                    heartRatecheck=false;
                    settigDrawable(R.drawable.heartrate_unselect,heartRate);
                }else{
                    heartRatecheck=true;
                    settigDrawable(R.drawable.heartrate_select,heartRate);
                }
                updta();
                break;
            case R.id.stride:
                if(stridecheck){
                    stridecheck=false;
                    settigDrawable(R.drawable.stride_unselect,stride);
                }else{
                    stridecheck=true;
                    settigDrawable(R.drawable.stride_select,stride);
                }
                updta();
                break;

            case R.id.iv_back:
                finish();
                break;
            case R.id.bottonstype:
                if(monthtype==0){
                    monthtype=1;
                    bottonstype.setText(getResources().getString(R.string.birthday_year));
                    QueryUserState(monthtype);
                }else{
                    monthtype=0;
                    bottonstype.setText(getResources().getString(R.string.birthday_month));
                    QueryUserState(monthtype);
                }
                break;

        }}



    Observable servable;
    private void QueryUserState( final int type) {
        Map<String, Object> map = new HashMap<>();
        if(type==0){
            map.put("uid", uid);//"uid" -> "111000036"
            map.put("month", DateUtil.getFormatTimeString(new Date().getTime(),"yyyy-MM"));//年-月
            //map.put("month", "2019-08");//年-月  queryYearRecord
            servable=  NetWorkUtil.getInstance().getJstyleApi().queryMonthRecord(map);
        }else{
            map.put("uid", uid);
            map.put("year", DateUtil.getFormatTimeString(new Date().getTime(),"yyyy"));//年-月
            servable=  NetWorkUtil.getInstance().getJstyleApi().queryYearRecord(map);
        }
        servable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Recordmian>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Recordmian activitvtiesData) {
                    if(null!=activitvtiesData.getData()){
                        if(null!=data){
                            data.clear();
                            data=null;}
                        data=activitvtiesData.getData();
                        updta();
                    }

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });

    }

    private void updta(){
        if(data==null){return;}
        //计算出最大值
        int max=getMaxValue(data);
        //设置视图的最高坐标

        ChartDataUtil.initDataChartView(ChartView_recordt, -0.5f, max==0?max + 100f:max + 15f, data.size()-0.3f, 0f);
        ChartView_recordt.setLineChartData(ChartDataUtil.getTrackREcord(GrowthRecordActivity.this, data,monthtype,max,distancecheck,pacecheck,heartRatecheck,stridecheck));
    }


    /**
     * 计算出最大值设置数据坐标
     */
    List<Integer> aa=new ArrayList<>();
    private int getMaxValue(List<RecordData>  activitvtiesData){
        aa.clear();
        for (int i=0;i<activitvtiesData.size();i++){
            aa.add((int)activitvtiesData.get(i).getDistance()) ;
            aa.add(activitvtiesData.get(i).getHeartRate()) ;
            aa.add(activitvtiesData.get(i).getPace()%256) ;//配速值%256得到分,值/256得到秒
            aa.add(activitvtiesData.get(i).getStride()) ;
        }
       return Collections.max(aa);
    }

    private void settigDrawable(int drawable,TextView view){
        Drawable dra = getResources().getDrawable(drawable);
        dra.setBounds(0, 0, dra.getMinimumWidth(), dra.getMinimumHeight());
        view.setCompoundDrawables(null, dra, null, null); //设置顶部图标
    }
}
